#!/usr/bin/ruby
class Parser
def initialize (links, lv, max_lv)
  @level=lv
  #m_logger=Logger.new("parse.log")
  for link in links
    begin
      logString="parser level #{@level} =>"+ link.uri.to_s
     # m_logger.info logString
      puts logString
     m = Mechanize.new 
     m.user_agent_alias = 'Mac Safari'
     pg = m.get(link.uri.to_s)
     appendToEmails(pg.body)
     lks = pg.links.uniq
     
     #kick out elements from links that were previously visited
    # lines = File.readlines("parse.log")
    # for line in lines
    #   ln=line.split("=>")[1]
    #   lks.delete(ln)
   #  end
       newLevel=@level+1
       Parser.new(lks, newLevel,max_lv )  if (newLevel <max_lv) && (lks.size>0)
   rescue
   end
  end
  
  
end

def appendToEmails(text)
  r = Regexp.new(/\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b/)     
  emails=text.scan(r).uniq
  if emails.size>0
  #lines = File.readlines("emails.txt")

  # for line in lines
  #   emails.delete(line)
 #  end
  
  open('emails.txt', 'a') { |f|
  for email in emails
    f.puts email
  end
}
end 
end
end




